package com.camtc.camcenter.base.controller;

import com.camtc.camcenter.base.bean.BaseQueryBean;
import com.camtc.camcenter.base.service.BaseService;
import com.camtc.camcenter.custom.CamHttpResult;
import com.camtc.camcenter.custom.MessageInfoEnum;
import com.camtc.camcenter.dao.entity.BaseEntity;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 通用Controller
 * </p>
 *
 * @author chentianshun
 * @since 2021-07-14
 */

@Slf4j
public abstract class BaseController<T extends BaseEntity, Q extends BaseQueryBean<T>> {

    /**
     * 项目起始地址
     */
    public static final String API_PATH = "camtc/v1/";
    /**
     * service基础对象
     */
    protected BaseService<T> baseService;

    /**
     * 初始化Service
     *
     * @return BaseService
     */
    public abstract BaseService<T> initBaseService();

    protected BaseController() {
        baseService = initBaseService();
    }

    /**
     * 简单分页查询
     *
     * @param queryBean 查询分页信息
     * @return
     */
    @ApiOperation(value = "泛用查询", notes = "非分页查询")
    @ApiImplicitParam(name = "queryBean", value = "泛用查询条件", dataType = "BaseQueryBean", dataTypeClass = BaseQueryBean.class, required = true)
    @ApiResponses(value = {@ApiResponse(code = 200, message = "{\n" +
            "    \"message\": \"success\",\n" +
            "    \"code\": 0\n" +
            " }", response = CamHttpResult.class)})
    public CamHttpResult queryPage(Q queryBean) {
        log.info("查询-{},查询对象是-{}", baseService.getClass().getSimpleName(), queryBean.getClass().getSimpleName());
        return CamHttpResult.ok(baseService.findByPage(queryBean.getQueryData(), queryBean.getPage(), queryBean.getRow()));
    }

    /**
     * 单表插入
     *
     * @param t 插入对象
     * @return 插入对象
     */
    public CamHttpResult insert(T t) {
        return CamHttpResult.ok(MessageInfoEnum.INSERT_INFO.getMessage(), baseService.insert(t));
    }

    /**
     * 单表修改
     *
     * @param t 修改对象
     * @return 修改后的对象
     */
    public CamHttpResult update(T t) {
        return CamHttpResult.ok(MessageInfoEnum.UPDATE_INFO.getMessage(), baseService.update(t));
    }

    /**
     * 单一删除
     *
     * @param id 主键
     * @return 成功信息
     */
    public CamHttpResult delete(String id) {
        baseService.delete(id);
        return CamHttpResult.ok(MessageInfoEnum.DELETE_INFO.getMessage());
    }
}



